{include file="public_head"}
{include file="public_menu"}
<div class="content_center">
  <div class="main_center">
    {include file="public_information_menu"}
    <div class="main_right right">
      <div class="main_right_title">设置支付密码</div>
      <div class="main_safety">
        <div class="center-pass">
          <div>
            <form action="" method="post" id="form">
              <div>
                <label>手机号</label>
                <input type="text" class="text" placeholder="请输入手机号" name="username" datatype="m11" nullmsg="请输入绑定手机号"
                       errormsg="请输入正确的手机号">
                <a href="javascript:check_num();" id="yzm">发送验证码</a>
              </div>
              <div>
                <label>验证码</label>
                <input type="text" class="text" placeholder="请输入验证码" name="code" datatype="n6" nullmsg="请输入验证码"
                       errormsg="验证码错误">
              </div>
              <div>
                <label>设置支付密码</label>
                <input type="password" class="text" placeholder="请设置6位数支付密码" name="pay_pass" datatype="n6"
                       nullmsg="请输入新的支付密码" errormsg="6位数字支付密码">
              </div>
              <div>
                <label>再次输入确认</label>
                <input type="password" class="text" placeholder="请再次输入确认" name="upwd" datatype="n6" nullmsg="请输入新的支付密码"
                       errormsg="6位数字支付密码">
              </div>
              <div>
                <input type="button" value="完成" class="submit" id="sub">
              </div>
            </form>
          </div>

        </div>
      </div>
    </div>
  </div>
</div>
<script>

  // 修改密码提交
  $(function () {
    $.Tipmsg.r = null;
    $.Tipmsg.p = null;
    $.Datatype.n6 = /^\d{6}$/;
    $.Datatype.m11 = /^1(3|4|5|7|8)\d{9}$/;
    // 表单验证
    $("#form").Validform({
      tiptype: function (msg) {
        layer.msg(msg, {icon: 2});
      },
      btnSubmit: '#sub',
      tipSweep: true,
      // ajax 提交
      ajaxPost: true,
      callback: function (t) {
        if (t.code == 200) {
          layer.msg('设置成功', {icon: 1});
          // 跳转页面
          setTimeout(function () {
            layer.closeAll('loading');
            // 跳转页面
            location.href = "{:url('account_security/index')}";
          }, 3000);
        } else {
          layer.msg(t.message, {icon: 2});
          setTimeout(function () {
            layer.closeAll('loading');
            // 跳转页面
//            location.reload();
          }, 3000);
        }

      }
    });
  });

  //发送验证码
  function check_num(act) {
    var tel = '';
    tel = $("[name='username']").val();
    if (tel == '') {
      layer.msg('请输入手机号！');
      return;
    }
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(tel))) {
      layer.msg('手机号格式不正确！');
      return;
    }

    if (tel != "{$find}") {
      layer.msg('此手机号非此账号绑定手机号！');
      return;
    }

    if (!check_unique(tel)) {
      return;
    }

    count_down();
    $.ajax({
      url: "{:url('shop/login/getVerif')}",
      dataType: 'json',
      type: 'post',
      data: {'tel': tel, 'act': 'pay_pass'},
      success: function (t) {
        if (t.code != 200) {
          layer.msg(t.message);
        }
      }
    })
  }

  //验证码倒计时
  function count_down() {
    var num = 60;
    var verif = $('#yzm');
    var timer = setInterval(function () {
        verif.text('倒计时'+num + '秒');
      if (num <= 60 && num >= 1) {
        verif.addClass('.disable');
        verif.removeAttr('href');
      } else {
        verif.text('重新获取验证码');
        window.clearInterval(timer);
        verif.attr("href", 'javascript:check_num();');
      }
      num--;
    }, 1000);
  }

  //检测新绑定的手机号码的正确性
  function check_unique(tel) {
    var flag = true;
    $.ajax({
      url: "{:url('home/login/bind_phone')}",
      type: 'post',
      async: false,
      dateType: 'json',
      data: {'tel': tel},
      success: function (t) {
        if (t.code != 200) {
          layer.msg(t.message);
          flag = false;
        }
      }
    });
    return flag;
  }
</script>
{include file="public_foot"}